package com.linln.admin.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {
    /**
     * 获取系统时间戳
     * @return
     */
    public long getCurTimeLong(){
        long time=System.currentTimeMillis();
        return time;
    }
    /**
     * 获取当前时间
     * @param pattern
     * @return
     */
    public static String getCurDate(String pattern){
        SimpleDateFormat sDateFormat = new SimpleDateFormat(pattern);
        return sDateFormat.format(new Date());
    }

    /**
     * 时间戳转换成字符窜
     * @param milSecond

     * @return
     */
    public static String getDateToString(long milSecond) {
        Date date = new Date(milSecond);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        return format.format(date);
    }

    /**
     * 将字符串转为时间戳
     * @param dateString
     * @param pattern
     * @return
     */
    public static long getStringToDate(String dateString, String pattern) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
        Date date = new Date();
        try{
            date = dateFormat.parse(dateString);
        } catch(ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return date.getTime();
    }

    public static String getDataAgo(long milSecond) {
        String day = "";
        try {
            Date date = new Date(milSecond);
//        long dayLong = System.currentTimeMillis() - milSecond;
            long dayString = getDayString(date);
            if (dayString == -1) {
                SimpleDateFormat format = new SimpleDateFormat("HH:mm");
                day = "昨天" + format.format(date);
            } else if (dayString == 0) {
                SimpleDateFormat format = new SimpleDateFormat("HH:mm");
                day = "今天" + format.format(date);
            } else {
                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
                day = format.format(date) + "";
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return day;
    }

    /**
     * @param startDate
     * @return // -2:前天             -1：昨天            0：今天             1：明天             2：后天
     */
    public static long getDayString(Date startDate){
        long day = -3;
        try {
            int offSet = Calendar.getInstance().getTimeZone().getRawOffset();
            long today = (System.currentTimeMillis()+offSet)/86400000;
            long start = (startDate.getTime()+offSet)/86400000;
            day =  start-today;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return day;
    }

}
